from sklearn import datasets
import pandas as pd
import numpy as np
# import  matplotlib.pyplot as plt
from sklearn import preprocessing
from sklearn.cluster import AgglomerativeClustering

#导入数据集
data = pd.read_excel('./data/cnew/data.xlsx')
data = np.array(data)
data_list = data.tolist()
iris_data=[]
for i in data_list:
    tt = []
    ttt = i[11:18]
    tt.append(max(ttt))
    tt.append(min(ttt))
    iris_data.append(tt)


# iris = datasets.load_iris()
# iris_data = iris.data
# print(iris_data)


def Max_MinNormalization(data,Max,Min):
    data = (data - Min) / (Max - Min)
    return data

l = ['85', '62', '74', '0.6', '13', '3', '46']

lt = []
lt.append(min(l))
lt.append(max(l))
iris_data.append(lt)

# 预处理
data = np.array(iris_data)
min_max_scaler = preprocessing.MinMaxScaler()
data_M = min_max_scaler.fit_transform(data)
# print(data_M)

ac = AgglomerativeClustering(n_clusters=60, affinity='euclidean', linkage='ward')
ac.fit(data_M)

labels = ac.fit_predict(data_M)
print(labels)

dic = {}

length = len(labels)
for i in range(length):
    if labels[i] in dic:
        dic[labels[i]].append(data_list[i][2])
    else:
        dic[labels[i]]=[]
        dic[labels[i]].append(data_list[i][2])

print(dic)

# resu = []
for i in dic.keys():
    if len(dic[i])>2:
        mmin = min(dic[i])
        mmax = max(dic[i])
        dic[i].remove(mmin)
        dic[i].remove(mmax)
        # resu.append((min(dic[i])+max(dic[i]))/2)
    elif len(dic[i])==1:
        # resu.append(dic[i][0])
        pass
    elif len(dic[i])==0:
        # resu.append(0)
        pass

print(dic)
